#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
        vector<int> res;
        int end = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == key)
            {
                for (int j = max(i - k, end); j <= i + k && j < nums.size(); j++)
                {
                    res.push_back(j);
                }
                end = i + k + 1;
            }
        }
        return res;
    }
};